import 'package:enter_prise_web/common/constants.dart';
import 'package:enter_prise_web/models/user.dart';
import 'package:enter_prise_web/page/edit_password.dart';
import 'package:enter_prise_web/page/staff/group_list.dart';
import 'package:enter_prise_web/page/index/index_controller.dart';
import 'package:enter_prise_web/page/staff/staff_controller.dart';
import 'package:enter_prise_web/page/view/calendar_view.dart';
import 'package:enter_prise_web/page/work/work_controller.dart';
import 'package:enter_prise_web/routes/app_routes.dart';
import 'package:enter_prise_web/widgets/side_menu.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class StaffSetting extends StatefulWidget {
  const StaffSetting({super.key, required this.user});
  final Users user;

  @override
  State<StaffSetting> createState() => _StaffSettingState();
}

class _StaffSettingState extends State<StaffSetting> {
  final WorkController workController = Get.find<WorkController>();

  final IndexController indexController = Get.find<IndexController>();
  final staffController controller = Get.find<staffController>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller.groupName.value = widget.user.groupName!;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置'),
        centerTitle: true,
      ),
      body: Column(
        children: [
          StaffListItem(
            text: Obx(() => Text(controller.groupName.value)),
            user: widget.user,
            title: "分组",
            name: widget.user.groupName.toString(),
            onTap: () {
              Get.to(GroupListIndex(
                isMove: true,
                uid: widget.user.uid!,
                groupName: widget.user.groupName.toString(),
              ));
            },
          ),
          StaffListItem(
            text: const Text(''),
            user: widget.user,
            title: "修改密码",
            onTap: () {
              Get.to(() => EditPwd(
                    uid: widget.user.uid!,
                  ));
            },
          ),
          StaffListItem(
            text: const Text(''),
            title: "到勤情况",
            user: widget.user,
            onTap: () {
              //跳转到签到页面
              Get.to(Calendar(
                name: widget.user.username!,
                uid: widget.user.uid!,
              ));
            },
          ),
          StaffListItem(
            text: const Text(''),
            title: "工作记录",
            user: widget.user,
            onTap: () {
              workController.uid.value = widget.user.uid!;
              workController.isSingle.value = true;

              NavigatorPage(
                  isSingle: true,
                  uid: widget.user.uid,
                  index: 3,
                  title: "记录页面(${widget.user.username})",
                  indexController: indexController,
                  workController: workController);
              Get.until((route) {
                if (route.settings.name == Routes.index) {
                  return true;
                } else {
                  return false;
                }
              });
            },
          ),
        ],
      ),
    );
  }
}

class StaffListItem extends StatelessWidget {
  const StaffListItem({
    super.key,
    required this.user,
    required this.title,
    this.tailingIcon = Icons.arrow_forward_ios,
    this.onTap,
    this.name,
    required this.text,
  });
  final Widget text;
  final Users user;
  final String title;
  final IconData? tailingIcon;
  final Function? onTap;
  final String? name;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 30),
      decoration: const BoxDecoration(
          border: Border(bottom: BorderSide(color: kGrayColor, width: 0.3))),
      padding: const EdgeInsets.only(top: 20, bottom: 20),
      child: GestureDetector(
        onTap: () async {
          onTap!();
        },
        child: Row(children: [
          Text(
            title,
            style: const TextStyle(fontSize: 25, color: Colors.black),
          ),
          const Spacer(),
          text,
          const SizedBox(
            width: 5,
          ),
          Icon(
            tailingIcon,
            color: kTextColor,
            size: 20,
          ),
        ]),
      ),
    );
  }
}
